const mongoose = require("mongoose");

//登录
const login = async ({  usersname, password }) => {
    const usersModel = mongoose.model("users");
    const data = await usersModel.findOne({usersname, password });
    let status = true;
    if (data) {
        return {status,data};
    } else {
        status = false;
        return status;
    }
};


//注册

const reg = async ({  usersname, password }) => {
    const usersModel = mongoose.model("users");
    const data = await usersModel.create({ usersname,  password })
    console.log(data);
    const admin = await usersModel.findOne({ _id: data._id });
    console.log(admin);
    let status = true;
    if (admin) {
        return status;
    } else {
        status = false;
        return status;
    }

};

// 添加订单
const addOrder = async ({str,scheduleId,tatol,_id}) => {
    const usersModel = mongoose.model("users");
    const users=await usersModel.findOne({_id});
    users.order.push([{str,scheduleId,tatol,_id}])
    console.log(users.order);
    // 添加文档到模型
    await usersModel.updateOne({_id},{order:users.order});
    const data = { status: true };
    return data;
}
// 获取用户
const getOne= async _id => {
    const usersModel = mongoose.model("users");
    // 添加文档到模型
    return await usersModel.findOne({_id});
}

//获取所有用户
const getAllUsers = async () => {
    const usersModel = mongoose.model("users");
    return await usersModel.find();
}

module.exports = { login, reg,addOrder ,getOne,getAllUsers}